package com.by.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ImageCode extends HttpServlet {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private int width =80; 
	private int height = 40; 
	private Font font = new Font("Arial", Font.BOLD, 24);
	private int count = 200;
	private int lineWidth= 4;
	private char[] selectChar =  new char[] { '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f',
			'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C',
			'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z' }; 
	@Override
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doGet(req, resp);
	}
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp)
		throws ServletException, IOException {
    	
	    resp.setHeader("Pragma", "No-cache");
	    resp.setHeader("Cache-Control", "no-cache");
	    resp.setDateHeader("Expires", 0);
		resp.setContentType("image/gif");
	
        
	    respVerifivationCode(req,resp);
    
    }
    /**
     *生成验证码图片
     * @param req
     * @param resp
     * @throws IOException 
     */
	private void respVerifivationCode(HttpServletRequest req,
			HttpServletResponse resp) throws IOException {
		
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
	
		Graphics2D g = (Graphics2D) image.getGraphics();

		g.setColor(getRandomColor(200,250));
		g.fillRect(0, 0, width, height);
	

		g.setFont(font);
		
		
		g.setColor(getRandomColor(0, 20));
		g.drawRect(0, 0, width-1, height-1);
		

		Random rm = new Random();
		
		for (int i = 0; i < count; i++) {
			
			g.setColor(getRandomColor(150, 200)); 
			
			int x = rm.nextInt(width - lineWidth - 1) + 1; 
			int y = rm.nextInt(height - lineWidth - 1) + 1;
			int xl = rm.nextInt(lineWidth);
			int yl = rm.nextInt(lineWidth);
			
			g.drawLine(x, y, x + xl, y + yl);
		}
		
		String sRand = "";
		for (int i = 0; i < 4; i++) {
			int charIndex = rm.nextInt(selectChar.length);
			String rand = String.valueOf(selectChar[charIndex]);
		    sRand += rand;
			
		  
			g.setColor(new Color(20 + rm.nextInt(130), 20 + rm.nextInt(130), 20 + rm.nextInt(130))); // --4--50-100

			g.drawString(rand, (13 * i) + 6, 25);
		}
		
		
		req.getSession().setAttribute(ImageCode.class.getName(),sRand);
		g.dispose();
		ImageIO.write(image, "PNG", resp.getOutputStream());
           		
	}
	private Color getRandomColor(int from, int to) {
		if(from>255){
			from = 255;
		}
		if(to>255)
		{
			to = 255;
		}
		Random rm = new Random();
		return new Color(from+rm.nextInt(to - from), from+rm.nextInt(to - from), from+rm.nextInt(to - from));
		
	}
}
